<template>
    <div class="shopcar-container">
        <!-- 购买的商品列表区域 -->
        <div class="goods-list">
            <div class="mui-card" v-for="(item,i) in goodslist" :key="item.id">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <mt-switch v-model="$store.getters.getgoodsSelected[item.id]"
                                   @change="goodSeclet(item.id,$store.getters.getgoodsSelected[item.id])"></mt-switch>
                        <img :src="item.thumb_path">
                        <div class="info">
                            <h1>{{item.title}}</h1>
                            <p class="goodsinfo">
                                <span class="sell_price">{{item.sell_price}}</span>
                                <number class="num" :initcount='$store.getters.getgoodsCount[item.id]'
                                        :goodsid='item.id'></number>
                                <!-- item.id用来删除main.js中的state中car里面对应id的数据（vuex数据存储仓种的数据） -->
                                <!-- i用来删除购物车中页面选中商品的数据 -->
                                <a href="#" class="del" @click.prevent='remove(item.id,i)'>删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 结算区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan">
                    <div class="content">
                        <p>总计(不含运费)</p>
                        <p>已勾选商品<span class="jian">{{ $store.getters.getGoodsCountAndMount.count }}</span>件，总价:<span
                                class="jian">￥{{ $store.getters.getGoodsCountAndMount.mount }}</span></p>
                    </div>
                    <mt-button type="danger">结算</mt-button>
                </div>
            </div>
        </div>
    </div>
</template>


<script>
    import number from '../subcomponents/shoppingcar_number.vue';

    export default {
        data() {
            return {
                goodslist: []
            }
        },
        created() {
            this.getGoodslist();
        },
        methods: {
            getGoodslist() {
                //拼接一个存储的所有商品id的数组
                var Addcountid = [];
                this.$store.state.car.forEach(item => {
                    Addcountid.push(item.id);
                });
                if (Addcountid.length <= 0) {
                    return;
                }
                this.$http.get("api/goods/getshopcarlist/" + Addcountid.join(",")).then(result => {
                    this.goodslist = result.body.message;
                })
            },
            remove(id, index) {
                //删除goodslist中的数据
                this.goodslist.splice(index, 1);
                //删除vuex中数据存储仓中的数据
                this.$store.commit("removefromcar", id);
            },
            goodSeclet(id, val) {
                this.$store.commit("updateSelect", {id, selected: val})
                // console.log(id+'-----'+val);
            }
        },
        components: {
            number
        }
    }


</script>


<style lang="scss" scoped>
    .shopcar-container {
        background-color: #eee;
        overflow: hidden;
        .goods-list {
            .mui-card-content-inner {
                display: flex;
                align-items: center;
            }
            img {
                width: 60px;
            }
            h1 {
                font-size: 13px;
            }
            .info {

                .sell_price {
                    color: red;
                    font-weight: bold;
                }
                .num {
                    height: 25px;
                }
                .del {
                    color: #007AFF;
                }
            }
        }
        .jiesuan {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .jian {
                color: #FF0000;
                font-weight: bold;
                font-size: 16px;
            }
        }
    }
</style>